package com.cpr.core.rabbitmq.springctl.anno;

import com.cpr.core.rabbitmq.springctl.anno.config.RabbitProducerConfig;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.nio.charset.StandardCharsets;

/**
 * @author Chen Panrui
 * created at 2022/3/15 23:43
 */
public class ProducerApp {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RabbitProducerConfig.class);
        RabbitTemplate template = context.getBean(RabbitTemplate.class);

        MessageProperties properties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("UTF-8")
                .setHeaderIfAbsent("id", "chenpanrui")
                .build();

        for (int i = 0; i < 1000; i++) {
            template.send("ex.anno.fanout",
                    "routing.anno",
                    MessageBuilder.withBody((i+"你好，Draven").getBytes(StandardCharsets.UTF_8)).andProperties(properties).build());
        }
        context.close();
    }
}
